package com.yoshon.yoshonapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.widget.AbsListView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yoshon.yoshonapp.expandlistview.Child;
import com.yoshon.yoshonapp.expandlistview.CustomELVAdapter;
import com.yoshon.yoshonapp.expandlistview.Group;
import com.yoshon.yoshonapp.expandlistview.PullToExpandableListView;
import com.yoshon.yoshonapp.expandlistview.PullToRefreshLayout;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {


    private PullToExpandableListView elv;
    private PullToRefreshLayout pullToRefreshLayout;

    private LinearLayout footView;


    /**
     * THIS DATA CAN BE FETCHED FROM DATABASES OR FROM WEB USING WEB API'S AND LOAD TO ADAPTER

     * **/

//    private List<String> groupname = new ArrayList<String>();
//
////    private static final String[] groupname = {"Bangalore","Mysore","Kodagu"};
//    private Map<String,List<String>> data = new HashMap<String,List<String>>();
////    private static final String[][] data = {{"Vidhanasouda","Cubbon park","Lalbagh"},
////            {"Palace","Chamundi Hills","Zoo"},
////            {"Abbey Falls","Talakaveri"}};
//
//    private Map<String,List<String>> listinfo = new HashMap<>();
////    private static final String[][] listinfo = {{"Dr Ambedkar rd,, Sampangi Ramnagar, Bangalore, Karnataka","Kasturba Road, Bangalore, Karnataka","Lal Bagh Road, Lalbagh, Mavalli, Bangalore, Karnataka"},
////            {"Sayyaji Rao Rd, Mysore, Karnataka","Mysore","Ittige gudu, Mysore, Karnataka"},
////            {"Kodagu,Karnataka","Kodagu,Karnataka"}};
//
//
//    private static final int[] ImgBckgrnd = {R.mipmap.bangalore,R.mipmap.mysore,R.mipmap.coorg};
//


    private List<Group> list = new ArrayList<>();

    private int lastItemIndex;
    private boolean isRefresh;

    private Handler mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case 0:
                        if (isRefresh){
                            isRefresh = false;
                            list.clear();
                        }else {
                            progressBar.setVisibility(View.GONE);
                        }
                        List<Group> list2 = new ArrayList<>();
                        for (int i = 0; i < 4; i++) {
                            Group g = new Group();
                            g.setImage(R.mipmap.bangalore);
                            g.setName("Yoshon"+i);
                            List<Child> childList = new ArrayList<>();
                            for (int j = 0; j < 3; j++) {
                                Child child = new Child();
                                child.setName("张三"+j);
                                childList.add(child);
                            }
                            g.setList(childList);
                            list2.add(g);
                        }
                        list.addAll(list2);
                        adapter.notify(list);
                        pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                        break;
                }
            }
        };
    private CustomELVAdapter adapter;
    private TextView foot_txt;
    private ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        elv  = (PullToExpandableListView) findViewById(R.id.lvExp1);
        elv.setFocusable(false);

        /**
         * THIS CAN BE USED IN ACTIVITY OR FRAGMENTS
         * **/
        initFootView();
        elv.addFooterView(footView);
        for (int i = 0; i < 4; i++) {
            Group g = new Group();
            g.setImage(R.mipmap.bangalore);
            g.setName("Yoshon"+i);
            List<Child> childList = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                Child child = new Child();
                child.setName("张三"+j);
                childList.add(child);
            }
            g.setList(childList);
            list.add(g);
        }

        adapter = new CustomELVAdapter(this, MainActivity.this, list);
        elv.setAdapter(adapter);
        pullToRefreshLayout = (PullToRefreshLayout) findViewById(R.id.pull);
        pullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                isRefresh = true;
                foot_txt.setText("正在加载...");
                mHandler.sendEmptyMessageDelayed(0,1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {

            }

            @Override
            public void onChangeState(int state) {

            }
        });

        elv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                 if(scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE
                        && lastItemIndex == adapter.getGroupCount() - 1){
                    //加载数据代码，此处省略了
                     progressBar.setVisibility(View.VISIBLE);
                    mHandler.sendEmptyMessageDelayed(0,1000);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                lastItemIndex = firstVisibleItem + visibleItemCount - 1 -1;
            }
        });

        elv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                                        int groupPosition, long id) {
                /**
                 * TODO:return true to enable group click
                 */

                // DO SOMETHING

                return false;
            }
        });

    }

    private void initFootView() {
        footView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.foot_view,null);
        foot_txt = (TextView) footView.findViewById(R.id.foot_txt);
        progressBar = (ProgressBar)  footView.findViewById(R.id.progress);
    }


}
